<script>
import NoContent from "./NoContent.vue";

export default {
  components: {NoContent},
   props: {
     keyword: {
       type: String,
     }
   },
  data() {
     return {
       nominates: [] , // 推荐列表
     }
  },
  watch: {
    keyword: function(newVal) {
      this.queryNominates(newVal);
    }
  },
  methods: {
    queryNominates(keyword) {
      return this.$api.queryResourcesBySearch(1, 10, keyword).then(ret=> {
        if (ret.status) {
          this.nominates = ret.data.list ;
        }
      });
    },
    extension(ext) {
      let supportExts = ["doc", "docx", "exe", "gif", "gz", "html", "md", "mp4", "pdf", "png", "ppt", "pptx", "rar", "rmvb", "txt", "wps", "xls", "xlsx", "zip", "7z", "avi", "bmp"];
      if (!supportExts.includes(ext)) {
        ext = "unknow";
      }
      // 在 Vue3 中 动态加载图片地址的方式
      return new URL("../assets/images/" + ext + ".svg", import.meta.url).href;
    },
  }
}
</script>

<template>
  <aside class="res-com" style="min-height: 695px">
    <h1>资源推荐:</h1>

    <NoContent :show="nominates.length == 0" />

    <div class="item" v-for="res in nominates">
      <img :src="extension(res.value.ext)">
      <div class="text">
        <a>{{ res.value.resourceName }}</a>
        <span>积分: <b>{{ res.value.score }}</b></span>
      </div>
    </div>

  </aside>
</template>

<style scoped>
.item {
  position: relative;
  height: 50px;
  padding: 5px 10px;
  margin: 5px 0 ;
  background: #fdfdfd;
}

.item img {
  width: 50px;
  height: 50px;
  border-radius: 10px;
}

.item .text {
  position: absolute;
  left: 60px;
  right: 5px;
  top: 0;
}

.item .text a, .item .text span {
  display: block;
  height: 30px;
  line-height: 30px;
  padding: 0 5px;
}

.item .text a {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item .text b {
  font-size: 20px;
  color: orange;
}
</style>